package user

import (
	"github.com/xinliangnote/go-gin-api/internal/code"
	"github.com/xinliangnote/go-gin-api/internal/pkg/core"
	"github.com/xinliangnote/go-gin-api/internal/pkg/validation"
	"net/http"
)

type ChangeStatusData struct {
	UserId string `form:"userId"`
	Status int    `form:"status"`
}

func (h *handler) ChangeStatus() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(ChangeStatusData)
		res := new(createResponse)
		if err := ctx.ShouldBindJSON(req); err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.ParamBindError,
				validation.Error(err)).WithError(err),
			)
			return
		}

		err := h.userService.ChangeStatus(ctx, req.UserId, req.Status)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.ChangeUserStatusError,
				code.Text(code.ChangeUserStatusError)).WithError(err),
			)
			return
		}

		res.Code = code.StatusOK
		res.Data = nil
		res.Msg = code.Text(code.StatusOK)
		ctx.Payload(res)
	}
}
